﻿using System;
using System.Collections.Generic;
using System.Linq;
using Domain.Entity;

namespace QQZoneMarketingDao
{
    public class QQAcountDao
    {
        /// <summary>
        /// 获取启用了的账号
        /// </summary>
        /// <returns></returns>
        public static List<QQAcount> GetEnabledAcountForZone()
        {
            using (var db = new DBDataContext())
            {
                return db.QQAcount.Where(x => x.EnableForZone).ToList();
            }
        }
        public static List<QQAcount> GetZoneIsLoginAcount()
        {
            using (var db = new DBDataContext())
            {
                return db.QQAcount.Where(x=>x.IsLogin).ToList();
            }
        }
        public static void SetZoneLoginState(string qq,bool islogin)
        {
            using (var db = new DBDataContext())
            {
                var ret = db.QQAcount.FirstOrDefault(x => x.QQ == qq);
                ret.IsLogin = islogin;
                if (islogin)
                    ret.LastLogin = DateTime.Now;
                db.SubmitChanges();
            }
        }

        public static bool SetQQAcountHasVisitedAdd(string qq)
        {
            using (var db = new DBDataContext())
            {
                var ret = db.QQAcount.FirstOrDefault(x => x.QQ == qq);
                ret.VisitedTimes ++;
                db.SubmitChanges();
                return true;
            }
        }
        public static bool SetAllQQAcountVisiteTimes(int hasvisite)
        {
            using (var db = new DBDataContext())
            {
                db.ExecuteCommand("UPDATE dbo.QQAcount SET VisitedTimes=" + hasvisite);
                db.SubmitChanges();
                return true;
            }
        }
        public static bool SetAllQQAcountMaxVisiteTimes(int maxvisite)
        {
            using (var db = new DBDataContext())
            {
                db.ExecuteCommand("UPDATE dbo.QQAcount SET MaxVisiteTimes=" + maxvisite);
                db.SubmitChanges();
                return true;
            }
        }
        public static bool SetQQAcountHasVisiteTimes(int visite,string qq)
        {
            using (var db = new DBDataContext())
            {
                var q = db.QQAcount.FirstOrDefault(x => x.QQ == qq);
                q.VisitedTimes = 0;
                db.SubmitChanges();
                return true;
            }
        }
    }
}
